<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>管理收货地址</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <script src="js/rem.js"></script>
    <script src="js/jquery.min.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="css/base.css"/>
    <link rel="stylesheet" type="text/css" href="css/page.css"/>
    <link rel="stylesheet" type="text/css" href="css/all.css"/>
    <link rel="stylesheet" type="text/css" href="css/mui.min.css"/>
    <link rel="stylesheet" type="text/css" href="css/loaders.min.css"/>
    <link rel="stylesheet" type="text/css" href="css/loading.css"/>
<script type="text/javascript">
	$(window).load(function(){
		$(".loading").addClass("loader-chanage")
		$(".loading").fadeOut(300)
	})
</script>
</head>
<!--loading页开始-->
<div class="loading">
	<div class="loader">
        <div class="loader-inner pacman">
          <div></div>
          <div></div>
          <div></div>
          <div></div>
          <div></div>
        </div>
	</div>
</div>
<!--loading页结束-->
	<body>
		<header class="mui-bar mui-bar-nav report-header box-s" id="header">
			<a href="center.html"><i class="iconfont icon-fanhui fl"></i></a>
			<p>管理收货地址</p>
	    </header>
	    <div id="main" class="mui-clearfix contanine tbody">
	    	<div class="addlist clearfloat">
	    		<div class="top clearfloat box-s">
	    			<ul>
	    				<li>
	    					<span class="fl">孙瑾晨</span>
	    					<span class="fr">1303505****</span>
	    				</li>
	    				<li>
	    					安徽省合肥市蜀山区高新技术产业开发区合肥市高新区长江西路拓基城市广场金座A幢2002
	    				</li>
	    			</ul>
	    		</div>
	    		<div class="bottom clearfloat box-s">
	    			<section class="shopcar clearfloat">
						<div class="radio fl">
						    <label>
						        <input type="radio" name="sex" value="">
						        <div class="option"></div>
						        <span class="mradd smradd fl">设为默认</span>
						    </label>
						</div>

						<div class="right fr clearfloat">
							<a href="#" class="fr">
								<i class="iconfont icon-lajixiang"></i>
								删除
							</a>
							<a href="#" class="fr">
								<i class="iconfont icon-shouji"></i>
								编辑
							</a>
						</div>
					</section>
	    		</div>
	    	</div>

	    	<div class="addlist clearfloat">
	    		<div class="top clearfloat box-s">
	    			<ul>
	    				<li>
	    					<span class="fl">孙瑾晨</span>
	    					<span class="fr">1303505****</span>
	    				</li>
	    				<li>
	    					安徽省合肥市蜀山区高新技术产业开发区合肥市高新区长江西路拓基城市广场金座A幢2002
	    				</li>
	    			</ul>
	    		</div>
	    		<div class="bottom clearfloat box-s">
	    			<section class="shopcar clearfloat">
						<div class="radio fl">
						    <label>
						        <input type="radio" name="sex" value="">
						        <div class="option"></div>
						        <span class="mradd smradd fl">设为默认</span>
						    </label>
						</div>

						<div class="right fr clearfloat">
							<a href="#" class="fr">
								<i class="iconfont icon-lajixiang"></i>
								删除
							</a>
							<a href="#" class="fr">
								<i class="iconfont icon-shouji"></i>
								编辑
							</a>
						</div>
					</section>
	    		</div>
	    	</div>

	    </div>

     	<a href="add-address.html" class="address-add fl">
     		添加新地址
     	</a>
		<script type="text/javascript">
			var template="<div class='addlist clearfloat'>"+
					"<div class='top clearfloat box-s'><ul><li>" +
					"<span class='fl'>[name]</span>"+
					"<span class='fr'>[phone]</span></li>"+
					"<li>[provinceName][cityName][areaName][address]</li></ul></div>"+
					"<div class='bottom clearfloat box-s'><section class='shopcar clearfloat'>" +
					"<div class='radio fl'><label>"+
					"<input type='radio' name='sex' value=''>"+
					"<div class='option btn-default' aid='[aid]' name='isDefault' ></div>"+
					"<span class='mradd smradd fl'>设为默认</span></label></div>"+
					"<div class='right fr clearfloat'>"+
					"<a aid='[aid]' class='fr btn-delete'><i class='iconfont icon-lajixiang'></i>删除</a>"+
					"<a aid='[aid]' href='/update-address.html?aid=[aid]' class='fr btn-info'><i  class='iconfont icon-shouji'></i>编辑"+
					"</a></div></section></div></div>";

			var addressListUrl="../addresses/list";
			$(function () {
				listAddr();

			});

			function listAddr(){
				$(".tbody").empty();
				$.get(addressListUrl,function(result){
					if(result.state==1000){
						for(var index in result.data){
							var addr=result.data[index];
							// js的replace默认仅替换第一个符合条件的内容
							var td=template.replace("[name]",addr.name)
									.replace("[phone]",addr.phone)
									.replace("[provinceName]",addr.provinceName)
									.replace("[cityName]",addr.cityName)
									.replace("[areaName]",addr.areaName)
									.replace("[address]",addr.address)
									.replace(/\[aid\]/g,addr.aid); // 替换所有aid
							var tdObj=$(td);
							var btnDefault=tdObj.find(".btn-default");
							if(addr.isDefault==1){
								btnDefault.addClass("optionchoosed");
							}
							$(".tbody").append(tdObj);
						}
					}else{
						alert(result.msg);
						location.replace("login.html");
					}
					// 为btn-default按钮添加点击事件
					$(".btn-default").bind("click",function(){
						// 获取当前组件的aid属性的值
						// this -> js对象 表示的是当前方法的调用者
						var aid=$(this).attr("aid");
						// 发送AJAX请求设置默认地址
						var setDefaultUrl="../addresses/"+aid+"/set_default";
						$.post(setDefaultUrl,function(result){
							if(result.state==1000){
								alert("默认地址设置成功");
								// 刷新地址数据
								listAddr();
							}else{
								alert(result.msg);
							}
						});
					});

					$(".btn-delete").bind("click",function(){
						var flag=confirm("是否删除该地址");
						if(flag==false){ // 取消
							return;
						}
						// 获取当前组件的aid属性的值
						// this -> js对象 表示的是当前方法的调用者
						var aid=$(this).attr("aid");
						// 发送AJAX请求设置默认地址
						var deleteUrl="../addresses/"+aid+"/delete";
						$.post(deleteUrl,function(result){
							if(result.state==1000){
								alert("删除地址成功");
								// 刷新地址数据
								listAddr();
							}else{
								alert(result.msg);
							}
						});
					});
				});
			}
		</script>
	</body>
	<script type="text/javascript" src="js/jquery-1.8.3.min.js" ></script>
<!--	<script src="js/fastclick.js"></script>-->
    <script src="https://cdn.bootcdn.net/ajax/libs/fastclick/1.0.5/fastclick.min.js"></script>
	<script src="js/mui.min.js"></script>
	<script type="text/javascript" src="js/hmt.js" ></script>
</html>
